package com.ruoyi.teaching.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.teaching.dto.AddHolidayDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.YaHolidayConfigMapper;
import com.ruoyi.teaching.domain.YaHolidayConfig;
import com.ruoyi.teaching.service.IYaHolidayConfigService;

/**
 * 节假日配置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-29
 */
@Service
public class YaHolidayConfigServiceImpl implements IYaHolidayConfigService {
  @Autowired
  private YaHolidayConfigMapper yaHolidayConfigMapper;

  @Autowired
  private StringRedisTemplate stringRedisTemplate;

  /**
   * 保存节假日配置到Redis
   *
   * @param year           年份
   * @param addHolidayDtos 节假日配置列表
   */
  private void saveHolidayToRedis(String year, List<AddHolidayDTO> addHolidayDtos) {
    // 删除原有数据
    stringRedisTemplate.delete("holiday:" + year);

    HashMap<String, String> holidayConfig = new HashMap<>();
    for (AddHolidayDTO addHolidayDTO : addHolidayDtos) {
      for (int i = 0; i < addHolidayDTO.getDays(); i++) {
        LocalDate cDate = addHolidayDTO.getStartDate().plusDays(i);
        holidayConfig.put(cDate.toString(), addHolidayDTO.getName());
      }
    }
    // 保存到Redis
    stringRedisTemplate.opsForHash().putAll("holiday:" + year, holidayConfig);
  }

  /**
   * 查询节假日配置
   *
   * @param id 节假日配置主键
   * @return 节假日配置
   */
  @Override
  public YaHolidayConfig selectYaHolidayConfigById(Long id) {
    return yaHolidayConfigMapper.selectYaHolidayConfigById(id);
  }

  /**
   * 查询节假日配置列表
   *
   * @param yaHolidayConfig 节假日配置
   * @return 节假日配置
   */
  @Override
  public List<YaHolidayConfig> selectYaHolidayConfigList(YaHolidayConfig yaHolidayConfig) {
    return yaHolidayConfigMapper.selectYaHolidayConfigList(yaHolidayConfig);
  }

  /**
   * 新增节假日配置
   *
   * @param yaHolidayConfig 节假日配置
   * @return 结果
   */
  @Override
  public int insertYaHolidayConfig(YaHolidayConfig yaHolidayConfig) {
    // 将获取到数据转换为对象
    String content = yaHolidayConfig.getContent();
    try {
      // 转换为JSON
      List<AddHolidayDTO> addHolidayDtos = JSON.parseArray(content, AddHolidayDTO.class);
      LocalDate startDate = addHolidayDtos.get(6).getStartDate();
      // 将日期转换为 "yyyy-mm-dd"
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
      String format = formatter.format(startDate);

      String year = format.split("-")[0];
      yaHolidayConfig.setYear(year);

      // # 避免重复新增 (查询 先删除在新增-根据年份删除即可)
      yaHolidayConfigMapper.deleteYaHolidayConfigByYear(year);
      saveHolidayToRedis(yaHolidayConfig.getYear(), addHolidayDtos);

    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    return yaHolidayConfigMapper.insertYaHolidayConfig(yaHolidayConfig);

  }

  /**
   * 修改节假日配置
   *
   * @param yaHolidayConfig 节假日配置
   * @return 结果
   */
  @Override
  public int updateYaHolidayConfig(YaHolidayConfig yaHolidayConfig) {
    //  删除redis中数据
    stringRedisTemplate.delete("holiday:" + yaHolidayConfig.getYear());
    // 添加
    String content = yaHolidayConfig.getContent();
    // 转换为JSON
    List<AddHolidayDTO> addHolidayDtos = JSON.parseArray(content, AddHolidayDTO.class);
    saveHolidayToRedis(yaHolidayConfig.getYear(), addHolidayDtos);

    return yaHolidayConfigMapper.updateYaHolidayConfig(yaHolidayConfig);
  }

  /**
   * 批量删除节假日配置
   *
   * @param ids 需要删除的节假日配置主键
   * @return 结果
   */
  @Override
  public int deleteYaHolidayConfigByIds(Long[] ids) {
    return yaHolidayConfigMapper.deleteYaHolidayConfigByIds(ids);
  }

  /**
   * 删除节假日配置信息
   *
   * @param id 节假日配置主键
   * @return 结果
   */
  @Override
  public int deleteYaHolidayConfigById(Long id) {
    return yaHolidayConfigMapper.deleteYaHolidayConfigById(id);
  }

}
